/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.yaba.dbs.core;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import java.io.FileInputStream;

/**
 *
 * @author emanuellfaustino
 */
public class YabaData {
    private List<Chunk> _chunks = new ArrayList<Chunk>();
    private String _name;
    private String _userId;
    private File _file = null;
    
    public YabaData(String userId, String name) {
        _name = name;
        _userId = userId;
    }
    
    public YabaData(String userId, File file) {
        _file = file;
        _name = file.getName();
        _userId = userId;
    }
    
    public void add(Chunk aChunk) {
        _chunks.add(aChunk);
    }
    
    public String getName() {
        return _name;
    }
    
    public String getUserId() {
        return _userId;    
    }
    
    public List<Chunk> getChunks(int size) {
        if(_file == null) {
            return _chunks;
        }
        
        _chunks = new ArrayList<Chunk>();
        
        byte[] contentBytes = getBytes();
        if(contentBytes != null) {
        	
        	int n = contentBytes.length / size;
        	int total = n;
        	
        	if(contentBytes.length % size != 0) {
        		total++;
        	}
        	
        	
        	if(n == 0 || n == 1) {
        		_chunks.add(new Chunk(_userId, 1, 1, contentBytes));
        		return _chunks;
        	}
        	
        	byte[] sizeFixed = new byte[size];
        	int y = 0;
        	for(int i = 0; i < n; i++) {
        		for(int j = 0; j < size; j++) {
        			sizeFixed[j] = contentBytes[y];
        			y++;
        		}
        		
        		_chunks.add(new Chunk(_userId, i+1, total, sizeFixed));
        		sizeFixed = new byte[size];
        	}
        	
        	if(y < contentBytes.length) {
        		sizeFixed = new byte[contentBytes.length - y];
        		for(int i = 0; y < contentBytes.length; i++) {
        			sizeFixed[i] = contentBytes[y];
        			y++;
        		}
        		
        		_chunks.add(new Chunk(_userId, total, total, sizeFixed));
        	}
        	
        	return _chunks;
        	
        }
        
        
        
        //for(int i = 0; i < contentBytes.length; i++) {
        //    byte[] byteChunk = new byte[1];
        //    byteChunk[0] = contentBytes[i];
            
        //    _chunks.add(new Chunk(_userId, i, contentBytes.length, byteChunk));
        //}
        Chunk chunk = new Chunk(_userId, 1, 1, contentBytes);
        _chunks.add(chunk);
        
        return _chunks;
    }
    
    public byte[] getBytes() {
        if(_file != null) {
            return getFileBytes();
        }
        
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        
        for(Chunk aChunk : _chunks) {
            try {
            	
                byteArray.write(aChunk.getBytes());
            
            } catch (IOException ex) {
               System.out.println(ex.getMessage());
                return null;
            }
            
        }
        
        return byteArray.toByteArray();
    }
    
    private byte[] getFileBytes() {
        if(_file == null) {
            return null;
        }
        
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();  
        FileInputStream input;
        try {
        	
        	byte[] buf = new byte[1024];
            
            input = new FileInputStream(_file);
           
            int readed = -1;
            while ((readed = input.read(buf, 0, buf.length)) != -1) {
                byteArray.write(buf, 0, readed);
            }
        
        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
            return null;
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
            return null;
        }
        
        return byteArray.toByteArray();
    }
}
